package com.canbot.u05.versionupdate

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.TextView

/**
 * 此对话框为艾诺中同样的对话框，可以设置标题，内容，两个按钮的内容， 同时可以设置是否显示倒计时自动关闭
 *
 * @author xiaowei
 */
class GeneralDialog : Dialog {

    private val COUNT_DOWN = 1

    private var mCountDown = 5

    private val mCountDownStr = "(${mCountDown}s后自动消失)"

    private var mContext: Context? = null

    private var mTitleText: String? = null

    private var mLeftBtnText: String? = null

    private var mRightBtnText: String? = null

    private lateinit var mMsgTv: TextView

    private var mString: String? = null // 待显示的文本信息

    private var mMsgText: String? = null // 包含倒计时显示的文本信息

    private var mIsShowCountdown: Boolean = false

    private var mClickListenerInterface: IButtonClickListerner? = null

    private val mBkBtnLeft = 0
    private val mBkBtnRight = 0 //按钮的背景资源ID

    @SuppressLint("HandlerLeak")
    private val mHandler = object : Handler() {
        override fun handleMessage(msg: android.os.Message) {
            when (msg.what) {
                COUNT_DOWN -> {
                    mCountDown--
                    if (mCountDown == 0) {
                        cancel()
                    } else {
                        mMsgText = "$mString(${mCountDown}s后自动消失)"
                        if (::mMsgTv.isInitialized) {
                            mMsgTv.text = mMsgText
                        }
                        this.sendEmptyMessageDelayed(COUNT_DOWN, (1 * 1000).toLong())
                    }
                }
            }
        }
    }

    /**
     * @param context
     * @param titleText
     * @param leftBtnText
     * @param rightBtnText
     * @param msgText
     * @param isShowCountdown 是否倒计时自动关闭
     * @param IClickListener
     */
    constructor(context: Context, titleText: String, leftBtnText: String, rightBtnText: String, msgText: String,
                isShowCountdown: Boolean, IClickListener: IButtonClickListerner) : super(context, R.style.centerdialog) {
        this.mContext = context
        this.mTitleText = titleText
        this.mString = msgText
        this.mLeftBtnText = leftBtnText
        this.mRightBtnText = rightBtnText
        this.mIsShowCountdown = isShowCountdown
        this.mMsgText = "$msgText$mCountDownStr"
        this.mClickListenerInterface = IClickListener
    }

    /**
     * 按钮按照默认的显示
     *
     * @param context
     * @param titleText
     * @param msgText
     * @param isShowCountdown
     * @param cLickListenerInterface
     */
    constructor(context: Context, titleText: String, msgText: String, isShowCountdown: Boolean, cLickListenerInterface: IButtonClickListerner) : super(context, R.style.centerdialog) {
        this.mContext = context
        this.mTitleText = titleText
        this.mString = msgText
        this.mMsgText = msgText + mCountDownStr
        this.mIsShowCountdown = isShowCountdown
        this.mClickListenerInterface = cLickListenerInterface
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    @SuppressLint("InflateParams")
    private fun initView() {
        val layoutInflater = LayoutInflater.from(mContext)
        val viewDialog = layoutInflater.inflate(R.layout.dialog_center2, null)

        setContentView(viewDialog)
        setCanceledOnTouchOutside(false)
        val titleTv = viewDialog.findViewById(R.id.tv_dialogtitle) as TextView
        titleTv.text = mTitleText
        val mBtnLeft = viewDialog.findViewById(R.id.btn_dialogleft) as Button
        val mBtnRight = viewDialog.findViewById(R.id.btn_dialogright) as Button
        mMsgTv = findViewById(R.id.tv_dialogmsg) as TextView
        if (mIsShowCountdown) {
            mMsgTv.text = mMsgText
        } else {
            mMsgTv.text = mString
        }
        if (mRightBtnText == null) {
            mBtnRight.text = "确定"
        } else {
            mBtnRight.text = mRightBtnText
        }

        if (mLeftBtnText == null) {
            mBtnLeft.text = "取消"
        } else {
            mBtnLeft.text = mLeftBtnText
        }
        mBtnLeft.setBackgroundResource(mBkBtnLeft)
        mBtnRight.setBackgroundResource(mBkBtnRight)
        mBtnLeft.setOnClickListener(ClickListener())
        mBtnRight.setOnClickListener(ClickListener())
    }

    override fun show() {
        super.show()
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        if (mIsShowCountdown) {
            mHandler.sendEmptyMessageDelayed(COUNT_DOWN, (1 * 1000).toLong())
        }
        fullScreenImmersive(window.decorView)
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    }

    override fun cancel() {
        super.cancel()
        if (mIsShowCountdown) {
            mHandler.removeMessages(COUNT_DOWN)
        }
    }

    private inner class ClickListener : View.OnClickListener {

        override fun onClick(v: View) {
            cancel()
            when (v.id) {
                R.id.btn_dialogright -> mClickListenerInterface?.doBtnRight()
                R.id.btn_dialogleft -> mClickListenerInterface?.doBtnLeft()
            }
        }

    }

    interface IButtonClickListerner {

        fun doBtnRight()

        fun doBtnLeft()
    }

    private fun fullScreenImmersive(decorView: View) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val uiOptions = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_FULLSCREEN)
            decorView.systemUiVisibility = uiOptions
        }
    }

}
